"""
Case Type   : Compatibility
Case Name   : stmt_name参数和已有的stmt_name重名;创建语句执行成功
Create At   : 2024/10/14
Owner       : liu-tong-8848
Description :
              1.创建prepare语句并执行
              2.创建prepare语句,stmt_name和已有的重名
              3.执行prepare语句
              4.清理环境
Expect      :
              1.成功
              2.成功
              3.返回step2结果
              4.清理环境成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class SQL(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.Constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbBmode')
        self.db_user_node = Node(node='PrimaryDbBmode')

    def test_add_dimension(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        step1 = '--step1:创建prepare语句并执行;expect:成功'
        self.log.info(step1)
        sql = "prepare a from 'select 1';execute a;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("1", result, "执行失败" + step1)
        self.assertNotIn("ERROR", result, "执行失败" + step1)

        step2 = '--step2:创建prepare语句,stmt_name和已有的重名;expect:成功'
        self.log.info(step2)
        sql = "prepare a from 'select 1';prepare a from 'select 2';"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertNotIn("ERROR", result, "执行失败" + step2)

        step3 = '--step3:执行prepare语句;expect:返回step2结果'
        self.log.info(step3)
        sql = "prepare a from 'select 1';prepare a from 'select 2';execute a;"
        self.log.info(sql)
        result = self.primary_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn("2", result, "执行失败" + step3)

    def tearDown(self):
        step4 = 'step4:清理环境 expect:清理环境成功'
        self.log.info(step4)
        clean_sql = "deallocate prepare a;"
        self.primary_sh.execut_db_sql(clean_sql)
        self.log.info("prepare语句无法跨会话生效，由于自动化用例会跨会话执行这些语句，所以自动化结束时无需清理")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
